package com.boot.security.service.authorization;

import com.boot.cache.redis.RedisCache;
import com.boot.security.model.po.Access;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.stereotype.Repository;

import java.util.Map;
import java.util.Set;

/**
 * @author 霜寒 <1621856595@qq.com>
 * @description 缓存 RequestMatcher -> Access 的映射
 * @date 2020/4/2 21:32
 **/

@Repository
public class AccessSourceCache extends RedisCache<String, Set<Access>> {

    private final String HASH_KEY = "boot:access:source";

    public AccessSourceCache(RedisConnectionFactory redisConnectionFactory, ObjectMapper om) {
        super(redisConnectionFactory, om, om.getTypeFactory().constructCollectionType(Set.class, Access.class));
    }

    public void putAll(Map<String, Set<Access>> map) {
        super.getRedisTemplate().opsForHash().putAll(HASH_KEY, map);
    }

    @SuppressWarnings("unchecked")
    @Override
    public Set<Access> get(String key) {
        return (Set<Access>) super.getRedisTemplate().opsForHash().get(HASH_KEY, key);
    }

    @Override
    public void put(String key, Set<Access> value) {
        super.getRedisTemplate().opsForHash().put(HASH_KEY, key, value);
    }

    @Override
    public void destroy() {
        clear();
    }

    @Override
    public void clear() {
        super.remove(HASH_KEY);
    }

}
